/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.swt.mto.thithu.service.impl;

import java.util.Date;
import java.util.List;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.swt.mto.thithu.model.Answer;
import com.swt.mto.thithu.model.Question;
import com.swt.mto.thithu.service.base.AnswerLocalServiceBaseImpl;

/**
 * The implementation of the answer local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link com.swt.mto.thithu.service.AnswerLocalService} interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
 * </p>
 *
 * @author tuan.nguyen
 * @see com.swt.mto.thithu.service.base.AnswerLocalServiceBaseImpl
 * @see com.swt.mto.thithu.service.AnswerLocalServiceUtil
 */
public class AnswerLocalServiceImpl extends AnswerLocalServiceBaseImpl {
	
	public List<Answer> getAnswerByQuestion(long questionId){
		List<Answer> answers = null;
		try {
			answers = answerPersistence.findByQuestionId(questionId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return answers;
	}
	
	public Answer addAnswer(Long answerId, Long createUserId,
			Long modifyUserId, Long questionId, int answerType, String description, 
			String correct, int answerPosition)
			throws PortalException, SystemException {

		Date now = new Date();
		Answer answer = null;
		answer = answerPersistence.fetchByPrimaryKey(answerId);
		if (answer == null) {
			answer = answerPersistence.create(answerId);
			answer.setCreateDate(now);
		} else {
			answer.setModifydate(now);
		}

		answer.setQuestionId(questionId);
		answer.setCreateUserId(createUserId == null ? 0 : createUserId);
		answer.setModifyUserId(modifyUserId == null ? 0 : modifyUserId);
		answer.setCorrect(correct);
		answer.setDecription(description);
		answer.setAnswerType(answerType);
		answer.setAnswerPosition(answerPosition);

		answerPersistence.update(answer, false);
		return answer;
	}
}